﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;


namespace G28BS.Models
{
    public class RankmkII
    {
        [Key]
        public int rankingId {get; set; }
        public int thisUserId { get; set; }
        public int tttRank { get; set; }
        public int fffRank { get; set; }
        // Actually really simple table - refactor onto another table? Current solution will get more complex as gametypes added, maybe leave as is       
        public RankmkII(UserProfile rankedUser)
        {
            rankingId = rankedUser.UserId;
            tttRank = RankingStats.tttStartRank;
            fffRank = RankingStats.fffStartRank;
        }

        // This is the only constructor I'm using, really.
        public RankmkII(string userName)
        {
            AppRepository repo = new AppRepository();

            this.thisUserId = (from UserProfile in repo.userProfiles where UserProfile.UserName == userName select UserProfile.UserId).FirstOrDefault();

            this.tttRank = RankingStats.tttStartRank;
            this.fffRank = RankingStats.fffStartRank;

        }

        // Declare default constructor to allow LINQ queries ( requires a default constructor )
        public RankmkII()
        {

        }

        public void changeTTTRank(int rankChange)
        {
            AppRepository repo = new AppRepository();

            this.tttRank += rankChange;

            if (this.tttRank > RankingStats.maxRank)
            {
                this.tttRank = RankingStats.maxRank;
            }
            else if (this.tttRank < RankingStats.minRank)
            {
                this.tttRank = RankingStats.minRank;
            }

            // write this unto the database, this might even be more stupid than it appears:

            //Rank thisGuyRank = (from Rank in repo.ranks where Rank.rankingID == this.rankingID select Rank).FirstOrDefault();

            //repo.AddRank(thisGuyRank);

            repo.Save();
        }

    }
}